home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / basic / ubppmpqs.zip / ppmp3a.asm < prev    next >
Assembly Source File  |  1992-05-24  |  16KB  |  1,071 lines

  1. ;PPMP3a.ASM 2.0
  2. ;    MACHINE LANGUAGE SUBROUTINES
  3. ;    FOR PPMPQS
  4. ;    1991/92 by YUJI KIDA
  5. ;
  6. .386P
  7.  
  8. code    segment    use16
  9.     assume cs:code,ds:code
  10.  
  11.  
  12.     INCLUDE    UBP.MAC
  13.     include    ppmp.h
  14.  
  15.     org    100h
  16. start:
  17.     JMP    START0
  18.  
  19.  
  20.     align    4
  21. R1_OFF    DW    ?
  22. R1_SEG    DW    ?
  23. R2_OFF    DW    ?
  24. R2_SEG    DW    ?
  25. X_OFF    DW    ?
  26. X_SEG    DW    ?
  27.  
  28. lpptop        dd    ?
  29. lppptr        dd    ?
  30. lppover        dd    ?
  31. lppcount    dd    ?
  32. baseXbytes    dw    ?,0
  33. baseunitbytes    dw    ?,0
  34.  
  35. basefileptr    dd    ?
  36. getreladr    dd    ?
  37.  
  38. basehandle    dw    ?
  39. combihandle    dw    ?
  40.  
  41. spmem        dw    ?
  42.  
  43. R1_buffer    db    4 dup(?)
  44. R2_buffer    db    4 dup(?)
  45. X_buffer    db    maxXbytes dup(?)
  46.  
  47. bufferptr    dw    ?
  48. buffer1        db    COMBIUNITBYTES dup (?)
  49. buffer2        db    COMBIUNITBYTES dup (?)
  50.  
  51. elimcount    dd    ?
  52. exchangesw    db    ?
  53.  
  54.  
  55. ;* branching
  56.  
  57.  
  58. START0:
  59.     MOV_AX    AR0        ;ARRAY[0] is the command number
  60.     MOV    BX,offset CMD_TBL
  61.     SHL    AX,1
  62.     ADD    BX,AX
  63.     JMP    CS:[BX]
  64.  
  65. CMD_TBL:
  66.     dw    ALLINIT,lppINIT,SETDATA,QSORT,ISORT
  67.     dw    ELIMINATION,GETRELATION,GETBASES,CLOSEHANDLE
  68.  
  69.  
  70. ; sweep down the '1'
  71.  
  72. sweep1:
  73.     mov    esi,[lpptop]
  74.     mov    edi,ebx            ;input from the main
  75.     mov    ebx,edi            ;last '1'
  76.     mov    edx,RAMUNITBYTES
  77. sweepLP:
  78.     mov    eax,1
  79.  
  80.     ;find '1' from the top
  81. sweep10:
  82.     add    esi,edx
  83.     cmp    eax,fs:[esi]
  84.     jne    sweep10
  85.  
  86.     ;find non '1' from the bottom
  87. sweep20:
  88.     sub    edi,edx
  89.     cmp    eax,fs:[edi]
  90.     je    sweep20
  91.     cmp    esi,edi
  92.     ja    sweepret
  93.  
  94.     mov    eax,fs:[esi]
  95.     xchg    eax,fs:[edi]
  96.     mov    fs:[esi],eax
  97.     mov    eax,fs:[esi+4]
  98.     xchg    eax,fs:[edi+4]
  99.     mov    fs:[esi+4],eax
  100.     mov    eax,fs:[esi+8]
  101.     xchg    eax,fs:[edi+8]
  102.     mov    fs:[esi+8],eax
  103.     mov    ebx,edi            ;last '1'
  104.     jmp    sweepLP
  105. sweepret:
  106.     ret
  107.  
  108.     
  109. ; check whether the sorting worked well
  110. check:
  111.     mov    esi,[lpptop]
  112.     add    esi,12
  113.     mov    ecx,[lppcount]
  114.     dec    ecx
  115. check10:
  116.     mov    eax,fs:[esi]
  117.     add    esi,12
  118.     cmp    eax,fs:[esi]
  119.     jb    check20
  120.     dec    ecx
  121.     jnz    check10
  122.     clc
  123.     ret
  124. check20:
  125.     stc
  126.     ret
  127.  
  128. ;
  129. ; close&delete base- and combination- files
  130. ; command#=8
  131.  
  132. CLOSEHANDLE:
  133.     mov    ax,cs
  134.     mov    ds,ax
  135.     mov    [spmem],sp
  136.  
  137.     mov    ah,3eh
  138.     mov    bx,[basehandle]
  139.     int    21h
  140.  
  141.     mov    ah,3eh
  142.     mov    bx,[combihandle]
  143.     int    21h
  144.  
  145.     mov    ah,41h
  146.     mov    dx,offset BASEFILENAME
  147.     int    21h
  148.  
  149.     mov    ah,41h
  150.     mov    dx,offset COMBIFILENAME
  151.     int    21h
  152.  
  153.     mov    ax,ss
  154.     mov    ds,ax
  155.     mov    es,ax
  156.     retf
  157.  
  158. ;
  159. ; get combination members
  160. ;
  161. GETBASES:
  162.     mov    ax,cs
  163.     mov    ds,ax
  164.  
  165.     mov    si,[bufferptr]
  166.     mov    dx,[si]
  167.     mov    cx,[si+2]    ;cx:dx = file ptr
  168.     mov    ax,dx
  169.     or    ax,cx
  170.     jz    getbasesnomore
  171.     add    si,4
  172.     mov    [bufferptr],si
  173.     mov    ah,42h        ;move file ptr
  174.     mov    al,0        ;absolute
  175.     mov    bx,[basehandle]
  176.     int    21h
  177.  
  178.     mov    ah,3fh        ;read handle
  179.     mov    dx,offset R1_buffer
  180.     mov    cx,[baseunitbytes]
  181.     int    21h
  182.  
  183.     ;set R1,R2,X,F
  184.  
  185.     les    di,dword ptr [R1_off]
  186.     mov    eax,dword ptr [R1_buffer]
  187.     mov    es:[di+2],eax
  188.     mov    cx,1
  189.     xor    ax,ax
  190.     or    eax,eax
  191.     jz    short getbase50
  192.     inc    cx
  193. getbase50:
  194.     mov    es:[di],cx
  195.  
  196.     les    di,dword ptr [R2_off]
  197.     mov    eax,dword ptr [R2_buffer]
  198.     mov    es:[di+2],eax
  199.     mov    cx,1
  200.     xor    ax,ax
  201.     or    eax,eax
  202.     jz    short getbase60
  203.     inc    cx
  204. getbase60:
  205.     mov    es:[di],cx
  206.  
  207.     les    di,dword ptr [X_off]
  208.     mov    si,offset X_buffer
  209.     mov    cx,[si]
  210.     and    cx,LENMASK
  211.     inc    cx
  212.     rep    movsw
  213.  
  214. getbasesret:
  215.     mov    ax,ss
  216.     mov    ds,ax
  217.     mov    es,ax
  218.     retf
  219.  
  220. getbasesnomore:
  221.     les    di,dword ptr [R1_off]    ;set R1=0
  222.     mov    word ptr es:[di],0
  223.     jmp    getbasesret
  224.  
  225.  
  226. ;
  227. ; find relation
  228. ; command#=6
  229.  
  230. GETRELATION:
  231.     mov    ax,cs
  232.     mov    ds,ax
  233.     mov    [spmem],sp
  234.  
  235. getrelationLP:
  236.     mov    ebx,[getreladr]
  237.     sub    ebx,RAMUNITBYTES
  238.     mov    [getreladr],ebx
  239.  
  240.     cmp    dword ptr fs:[ebx],1
  241.     jne    short nomorerelation
  242.  
  243.     mov    eax,fs:[ebx+8]
  244.     or    eax,eax
  245.     jz    getrelationLP        ;erased data
  246.  
  247.     mov    dx,offset buffer1
  248.     call    getcombi
  249.     mov    si,offset buffer1
  250.     mov    [bufferptr],si
  251. getrelation50:
  252.     add    si,4
  253.     cmp    dword ptr [si-4],COMBIMASK
  254.     jb    getrelation50
  255.     xor    dword ptr [si-4],COMBIMASK
  256.     mov    dword ptr [si],0    ;end mark
  257.  
  258. getrelationret:
  259.     mov    ax,ss
  260.     mov    ds,ax
  261.     retf
  262.  
  263. nomorerelation:
  264.     mov    bx,AR0
  265.     mov    word ptr [bx],0
  266.     jmp    getrelationret
  267.  
  268.         
  269. ;
  270. ; pseudo-Gaussian elimination
  271. ; command#=5
  272.  
  273. ELIMINATION:
  274.     mov    ax,cs
  275.     mov    ds,ax
  276.     mov    [spmem],sp
  277. elimIN:
  278.     mov    [exchangesw],0
  279.     mov    ebx,[lpptop]
  280.     add    ebx,RAMUNITBYTES
  281.  
  282. elimLP1:
  283.     mov    eax,fs:[ebx]    ;R
  284.     cmp    eax,1
  285.     je    short elimdone
  286.     mov    esi,ebx
  287. elimLP2:
  288.     add    esi,RAMUNITBYTES
  289.     cmp    eax,fs:[esi]    ;next R
  290.     je    short elimmatch
  291.     mov    ebx,esi
  292.     jmp    short elimLP1
  293.  
  294. elimdone:
  295.     call    sweep1        ;sweep down the '1's (use ebx)
  296.     mov    eax,ebx
  297.     sub    eax,[lpptop]
  298.     xor    edx,edx
  299.     mov    ecx,RAMUNITBYTES
  300.     div    ecx
  301.     dec    eax
  302.     mov    [elimcount],eax
  303.  
  304.     cmp    [exchangesw],0
  305.     je    elim_alldone
  306.  
  307.     mov    ecx,eax
  308.     push    ecx
  309.     mov    ebx,1
  310.     call    qsortmain
  311.     pop    ecx
  312. ;    call    ssortmain
  313.     call    isortmain
  314. ;    call    check
  315.     jmp    elimIN
  316.  
  317. elim_alldone:
  318.     call    number_relations
  319.  
  320.     mov    eax,[lppover]
  321.     mov    [getreladr],eax
  322.  
  323.     lds    si,dword ptr [X_OFF]
  324.     mov    [si+2],ecx
  325.     xor    ax,ax
  326. elim90:
  327.     jecxz    short elim100
  328.     inc    ax
  329.     shr    ecx,16
  330.     jmp    elim90
  331. elim100:    
  332.     mov    [si],ax
  333.  
  334.     mov    ax,ss
  335.     mov    ds,ax
  336.     mov    es,ax
  337.     retf
  338.  
  339. elimmatch:
  340.     mov    [exchangesw],-1
  341. elimmatchLP:
  342.     mov    ecx,fs:[ebx+4]
  343.     mov    edx,fs:[esi+4]
  344.     cmp    ecx,edx
  345.     je    short fullmatch
  346. ;    cmp    eax,fs:[esi+RAMUNITBYTES]
  347. ;    jne    short halfmatch
  348. ;    cmp    edx,fs:[esi+RAMUNITBYTES+4]
  349. ;    je    short fullmatch_next
  350. halfmatch:
  351.     mov    fs:[esi],edx    ;new R1 = max of old R2 & R2'
  352.     mov    fs:[esi+4],ecx    ;new R2 = min of old R2 & R2'
  353.     jmp    short rewritecombi
  354.  
  355. fullmatch:
  356.     mov    dword ptr fs:[esi],1    ;new R1 = 1
  357.     mov    dword ptr fs:[esi+4],1    ;new R2 = 1
  358. rewritecombi:
  359.     push    ebx            ;/*
  360.     mov    eax,fs:[esi+8]
  361.     mov    edx,offset buffer1
  362.     call    getcombi
  363.     pop    ebx            ;*/
  364.     push    ebx            ;/*
  365.     mov    eax,fs:[ebx+8]
  366.     mov    edx,offset buffer2
  367.     call    getcombi
  368.     push    si            ;/**
  369.     call    linkcombi
  370.     pop    si            ;**/
  371.     sub    di,offset buffer1
  372.     cmp    di,COMBIUNITBYTES
  373.     ja    short toolonglink
  374.     call    putnewcombi
  375.     mov    fs:[esi+8],eax        ;new file ptr
  376.     pop    ebx            ;*/
  377.     jmp    elimLP2
  378.  
  379. fullmatch_next:
  380.     push    ebx            ;/*
  381.     mov    dword ptr fs:[esi],1    ;new R1 = 1
  382.     mov    dword ptr fs:[esi+4],1    ;new R2 = 1
  383.     mov    eax,fs:[esi+8]
  384.     mov    edx,offset buffer1
  385.     call    getcombi
  386.     mov    eax,fs:[esi+RAMUNITBYTES+8]
  387.     mov    edx,offset buffer2
  388.     call    getcombi
  389.     push    si            ;/**
  390.     call    linkcombi
  391.     pop    si            ;**/
  392.     sub    di,offset buffer1
  393.     cmp    di,COMBIUNITBYTES
  394.     ja    short toolonglink
  395.     call    putnewcombi
  396.     mov    fs:[esi+8],eax        ;new ptr
  397.     pop    ebx            ;*/
  398.     mov    eax,fs:[ebx]
  399.     add    esi,RAMUNITBYTES
  400.     jmp    elimmatchLP
  401.  
  402. toolonglink:
  403.     mov    dword ptr fs:[esi],1    ;erased marks
  404.     mov    dword ptr fs:[esi+4],1    ;
  405.     mov    dword ptr fs:[esi+8],0    ;
  406.     pop    ebx
  407.     jmp    elimLP2
  408.  
  409. getcombi:
  410.     cmp    eax,COMBIMASK
  411.     jae    short getcombidirect
  412.  
  413.     push    dx
  414.     mov    dx,ax
  415.     shr    eax,16
  416.     mov    cx,ax        ;cx:dx = file ptr
  417.     mov    ah,42h        ;move pointer
  418.     mov    al,0        ;absolute
  419.     mov    bx,[combihandle]
  420.     int    21h
  421.  
  422.     pop    dx
  423.     mov    ah,3fh        ;read handle
  424.     mov    cx,COMBIUNITBYTES
  425.     int    21h
  426.     ret
  427. getcombidirect:
  428.     mov    bx,dx
  429.     mov    [bx],eax
  430.     ret
  431.  
  432.  
  433. linkcombi:
  434.     mov    di,offset buffer1 - 4
  435.     mov    si,offset buffer2
  436. linkcombi10:
  437.     add    di,4
  438.     cmp    dword ptr [di],COMBIMASK
  439.     jb    linkcombi10
  440.     xor    dword ptr [di],COMBIMASK
  441.     add    di,4
  442. linkcombi20:
  443.     mov    eax,[si]
  444.     mov    [di],eax
  445.     add    si,4
  446.     add    di,4
  447.     cmp    eax,COMBIMASK
  448.     jb    linkcombi20
  449.     ret                ;di = next of endmark
  450.     
  451. putnewcombi:
  452.     push    di        ;/* size
  453.     mov    ah,42h        ;move pointer
  454.     mov    al,2        ;to the end
  455.     mov    bx,[combihandle]
  456.     xor    cx,cx
  457.     xor    dx,dx
  458.     int    21h
  459.  
  460.     pop    cx        ;*/
  461.     push    dx        ;/* dx:ax = new ptr
  462.     push    ax        ;/**
  463.  
  464.     mov    dx,offset buffer1
  465.     mov    ah,40h        ;write handle
  466.     int    21h
  467.     cmp    ax,cx
  468.     jne    short combidiskfull
  469.     pop    eax        ;**/ */ eax = new ptr
  470.     clc
  471.     ret
  472.  
  473. writeinitcombi:
  474.     mov    dx,offset buffer1
  475.     mov    ah,40h        ;write handle
  476.     mov    bx,[combihandle]
  477.     mov    cx,4
  478.     int    21h
  479.     cmp    ax,cx
  480.     jne    short combidiskfull
  481.     clc
  482.     ret
  483.  
  484. combidiskfull:
  485.     mov    word ptr ds:[AR0],8001h
  486.     mov    sp,cs:[spmem]
  487.     mov    ax,ss
  488.     mov    ds,ax
  489.     mov    es,ax
  490.     retf
  491.  
  492.  
  493. number_relations:
  494.     mov    eax,[elimcount]
  495.     mov    edx,RAMUNITBYTES
  496.     mul    edx
  497.     add    eax,[lpptop]
  498.     mov    ebx,eax
  499.     xor    ecx,ecx
  500. numrel10:
  501.     add    ebx,RAMUNITBYTES
  502.     cmp    ebx,[lppover]
  503.     jae    short numrel100
  504.  
  505.     cmp    dword ptr fs:[ebx+8],0
  506.     je    numrel10        ;erased data
  507.     inc    ecx
  508.     jmp    numrel10
  509. numrel100:
  510.     mov    eax,ecx
  511.     ret            ;eax = ecx = result
  512.  
  513.  
  514. ;
  515. ; insertion sort wrt R1 as 1stKey and R2 as 2ndKey
  516. ; command#=4
  517.  
  518. copySIDI    macro
  519.     mov    eax,fs:[esi]    ;12 bytes
  520.     mov    fs:[edi],eax
  521.     mov    eax,fs:[esi+4]
  522.     mov    fs:[edi+4],eax
  523.     mov    eax,fs:[e